package com.kele.volateTest;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kele
 * @date 2021/10/13 15:10
 * @description
 *
 * *  volatile
 *   可见性
 *   不能保证原子性
 *  防止指令重排
 */
public class NoAutomic {

    private volatile   static AtomicInteger num=new AtomicInteger();


        public  static void   add(){

            try {
               num.incrementAndGet();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        }


    public static void main(String[] args) {
        for (int i = 1; i <=20 ; i++) {
            new Thread(()->{
                for (int j = 0; j <1000 ; j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount()>2){
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName()+" : "+num);
    }
}
